//
//  RepaymentCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/10.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class RepaymentCell: UITableViewCell {
    
    fileprivate let fontSize = UIFont.systemFont(ofSize: RATIO(maxNum: 16))

    fileprivate let bankImageView = UIImageView() //银行图片
    fileprivate let bankLabel = UILabel() //银行名称
    fileprivate let numberLabel = UILabel() //银行卡号
    fileprivate let phoneLabel = UILabel() //手机号码
    fileprivate let dateLabel = UILabel() //还款日
    fileprivate let dayLabel = UILabel() //提前天数
    fileprivate let contentLabel = UILabel() //提醒内容
    
    var model : RepaymentModel! {
        didSet{
            getData()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        let bgView = UIView()
        bgView.layer.contents = #imageLiteral(resourceName: "hktxbg").cgImage
        self.contentView.addSubview(bgView)
        
        
        bgView.addSubview(bankImageView)
        
        bankLabel.font = fontSize
        bgView.addSubview(bankLabel)
        
        numberLabel.font = fontSize
        bgView.addSubview(numberLabel)
        
        let phoneTitleLabel = UILabel()
        phoneTitleLabel.text = phoneNumber + ":"
        phoneTitleLabel.font = fontSize
        bgView.addSubview(phoneTitleLabel)
        
        let dateTitleLabel = UILabel()
        dateTitleLabel.text = repaymentDate + ":"
        dateTitleLabel.font = fontSize
        bgView.addSubview(dateTitleLabel)
        
        let dayTitleLabel = UILabel()
        dayTitleLabel.text = daysInAdvance + ":"
        dayTitleLabel.font = fontSize
        bgView.addSubview(dayTitleLabel)
        
        let contentTitleLabel = UILabel()
        contentTitleLabel.text = reminderContent + ":"
        contentTitleLabel.font = fontSize
        bgView.addSubview(contentTitleLabel)
        
        phoneLabel.font = fontSize
        bgView.addSubview(phoneLabel)
        
        dateLabel.font = fontSize
        bgView.addSubview(dateLabel)
        
        dayLabel.font = fontSize
        bgView.addSubview(dayLabel)
        
        contentLabel.font = fontSize
        bgView.addSubview(contentLabel)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.contentView.addSubview(lineView)
        
        bgView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.bottom.equalToSuperview().offset(RATIO_H(maxNum: -10))
        }
        
        bankImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.centerX.equalTo(getCenterX(count: 87))
            make.width.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        bankLabel.snp.makeConstraints { (make) in
            make.top.equalTo(bankImageView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.centerX.equalTo(bankImageView)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        numberLabel.snp.makeConstraints { (make) in
            make.top.equalTo(bankLabel.snp.bottom)
            make.centerX.equalTo(bankLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(bankLabel)
        }
        
        phoneTitleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.centerX.equalTo(getCenterX(count: 275))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        dateTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(phoneTitleLabel.snp.bottom)
            make.centerX.equalTo(phoneTitleLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(phoneTitleLabel)
        }
        
        dayTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(dateTitleLabel.snp.bottom)
            make.centerX.equalTo(dateTitleLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(dateTitleLabel)
        }
        
        contentTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(dayTitleLabel.snp.bottom)
            make.centerX.equalTo(dayTitleLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(dayTitleLabel)
        }
        
        phoneLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.centerX.equalTo(getCenterX(count: 478))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        dateLabel.snp.makeConstraints { (make) in
            make.top.equalTo(phoneLabel.snp.bottom)
            make.centerX.equalTo(phoneLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(phoneLabel)
        }
        
        dayLabel.snp.makeConstraints { (make) in
            make.top.equalTo(dateLabel.snp.bottom)
            make.centerX.equalTo(dateLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(dateLabel)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(dayLabel.snp.bottom)
            make.centerX.equalTo(dayLabel)
            make.width.lessThanOrEqualTo(RATIO(maxNum: 100))
            make.height.equalTo(dayLabel)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalTo(bgView)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    //MARK: 获取中心点
    fileprivate func getCenterX(count:CGFloat) -> CGFloat {
        
        let proportion = count/568
        let width = (ScreenW-RATIO(maxNum: 20)) * proportion
        
        return width
    }
    
    //MARK: 填充数据
    fileprivate func getData() {
        
        let image = KMTool.selectBankImage(bankName: model.bank_name ?? "")
        bankImageView.image = UIImage.imageWithName(image)
        bankLabel.text = model.bank_name
        numberLabel.text = model.back_phone
        phoneLabel.text = model.phone
        dateLabel.text = perMonth + model.huan_day! + day
        dayLabel.text = model.day_num! + days
        contentLabel.text = model.content2
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

}
